package com.yonyou.pmclouds.signin.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.basecom.util.SqlUtil;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.signin.entity.SignInListViewVO;
import com.yonyou.pmclouds.signin.entity.SignInVO;
import com.yonyou.pmclouds.signin.mapper.SignInMapper;
import com.yonyou.pmclouds.signin.rmiitf.SignInQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= SignInQuery.class)
public class SignInQueryImpl implements SignInQuery{

    @Autowired
    private SignInMapper mapper;
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;


    @Override
    public Map<String, List<List<SignInListViewVO>> > getSignInListInfo(String pkTenant, Date start, Date end, String[] pkUsers) throws BusinessException {
        Map<String, List<List<SignInListViewVO>> > resultMap = new HashMap<>();
        if(StringUtils.isEmpty(pkTenant) || start == null || end == null || ArrayUtils.isEmpty(pkUsers)){
            return resultMap;
        }
        SignInVO[] resultArray = mapper.getListViewVOByUsers(pkTenant, start, end, SqlUtil.getSqlIn(pkUsers));
        if(ArrayUtils.isEmpty(resultArray)){
            return resultMap;
        }
        Map<String, String> projectIdToName = queryProjectNames(resultArray);
        Date preTime = null;
        for(int i = 0; i < resultArray.length; i++){
            SignInVO vo = resultArray[i];
            if(vo != null && vo.getCreator() != null){
                if(resultMap.get(vo.getCreator()) == null){
                    resultMap.put(vo.getCreator(), new ArrayList<List<SignInListViewVO>>());
                }
                if(preTime == null || preTime.after(vo.getCreationtime()) || resultMap.get(vo.getCreator()).size() == 0){
                    resultMap.get(vo.getCreator()).add(new ArrayList<SignInListViewVO>());
                    preTime = DateUtils.asDateBegin(vo.getCreationtime());
                }
                SignInListViewVO viewVO = new SignInListViewVO(vo.getCreationtime(), vo.getPosition());
                // 设置项目名
                if (StringUtils.isNotEmpty(vo.getPkProject())) {
                    viewVO.setProjectName(projectIdToName.get(vo.getPkProject()));
                }
                resultMap.get(vo.getCreator()).get(resultMap.get(vo.getCreator()).size() - 1).add(viewVO);
            }
        }
        return resultMap;
    }

    @Override
    public Map<String, List<SignInListViewVO>> getSignInListTotalInfo(String pkTenant, Date start, Date end, String[] pkUsers) throws BusinessException {
        Map<String, List<SignInListViewVO>> resultMap = new HashMap<>();
        if(StringUtils.isEmpty(pkTenant) || start == null || end == null || ArrayUtils.isEmpty(pkUsers)){
            return resultMap;
        }
        SignInVO[] resultArray = mapper.getListViewVOByUsers(pkTenant, start, end, SqlUtil.getSqlIn(pkUsers));
        if(ArrayUtils.isEmpty(resultArray)){
            return resultMap;
        }
        Map<String, String> projectIdToName = queryProjectNames(resultArray);
        for(SignInVO vo : resultArray){
            if(vo != null && vo.getCreator() != null){
                if(resultMap.get(vo.getCreator()) == null){
                    resultMap.put(vo.getCreator(), new ArrayList<SignInListViewVO>());
                }
                SignInListViewVO viewVO = new SignInListViewVO(vo.getCreationtime(), vo.getPosition());
                // 设置项目名
                if (StringUtils.isNotEmpty(vo.getPkProject())) {
                    viewVO.setProjectName(projectIdToName.get(vo.getPkProject()));
                }
                resultMap.get(vo.getCreator()).add(viewVO);
            }
        }
        return resultMap;
    }

    /**
     * 查询项目名称
     *
     * @param resultArray
     * @return
     * @throws BusinessException
     */
    private Map<String, String> queryProjectNames(SignInVO[] resultArray) throws BusinessException {
        Set<String> projectSet = new HashSet<>();

        for (SignInVO vo : resultArray)
            projectSet.add(vo.getPkProject());

        Map<String, String> projectIdToName = projectSimpleQuery.queryProjectNameByIds(projectSet.toArray(new String[projectSet.size()]));
        return projectIdToName;
    }
}
